package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.POS;
import data.POSCategory;

public class POSCategoryLogic extends AbstractBL {
	public POSCategoryLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<POSCategory> GeneratePOSCategory(long hotelId){
		List<POSCategory> list = new ArrayList<POSCategory>();
		POSLogic posLogic = new POSLogic(req,resp);
		POS iChic = posLogic.GetPOS(hotelId, "iChic");
		POS iCoffee = posLogic.GetPOS(hotelId, "iCoffee");
		
		POSCategory item;
		if(GetPOSCategory(iCoffee.getId(),"Snack") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Snack");
			item.setPOSId(iCoffee.getId());
			list.add(item);
		}
		if(GetPOSCategory(iCoffee.getId(),"Beverage") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Beverage");
			item.setPOSId(iCoffee.getId());
			list.add(item);
		}
		if(GetPOSCategory(iCoffee.getId(),"iCoffee") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("iCoffee");
			item.setPOSId(iCoffee.getId());
			list.add(item);
		}

		if(GetPOSCategory(iChic.getId(),"Steak,Salad,Soup") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Steak,Salad,Soup");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Thai Salad-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Thai Salad-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Thai Soup-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Thai Soup-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Fried Food-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Fried Food-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Food-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Food-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Entree Over Rice-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Entree Over Rice-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Appatiser-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Appatiser-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Dessert-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Dessert-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Drink") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Drink");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Extra") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Extra");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Breakfast-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Breakfast-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Ingredient-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Ingredient-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Employee's Food") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Employee's Food");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		if(GetPOSCategory(iChic.getId(),"Other-") == null){
			item = new POSCategory();
			item.setHotelId(hotelId);
			item.setTitle("Other-");
			item.setPOSId(iChic.getId());
			list.add(item);
		}
		return list;
	}
	
	public POSCategory GetPOSCategory(long id)
	{
		Key key = KeyFactory.createKey(EntityName.POSCategory, id);
		try {
			Entity lang = datastore.get(key);
			POSCategory c = new POSCategory();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public POSCategory GetPOSCategory(Entity entity)
	{
		if(entity == null) return null;
		POSCategory item = new POSCategory();
		item.setEntity(entity);
		return item;
	}
	
	public POSCategory GetPOSCategory(long posId,String title){
		return GetPOSCategory(posId,title,0);
	}
	
	public POSCategory GetPOSCategory(long posId,String title,long parentCategory)
	{
		Query q = new Query(EntityName.POSCategory);
		Filter filterPosId = new FilterPredicate("posId",FilterOperator.EQUAL,posId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);
		Filter filterParentCat = new FilterPredicate("parentCategory",FilterOperator.EQUAL,parentCategory);
		
		Filter filter = CompositeFilterOperator.and(filterPosId, filterStatus, filterAvaliable,filterTitle,filterParentCat);
		
		q.setFilter(filter);
		
		return GetPOSCategory(datastore.prepare(q).asSingleEntity());
	}
	
	public List<POSCategory> GetAllCategoriesLimit(long hotelId,long offset,long line,String posId)
	{
		Query q = new Query(EntityName.POSCategory);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterparentCategory = new FilterPredicate("parentCategory",FilterOperator.EQUAL,0);
		Filter filterposId = new FilterPredicate("posId",FilterOperator.EQUAL,Long.parseLong(posId));
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterparentCategory,filterposId);
		q.setFilter(filter);
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	public List<POSCategory> GetAllCategories(long hotelId,String posId)
	{
		Query q = new Query(EntityName.POSCategory);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterparentCategory = new FilterPredicate("parentCategory",FilterOperator.EQUAL,0);
		Filter filterposId = new FilterPredicate("posId",FilterOperator.EQUAL,Long.parseLong(posId));
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterparentCategory, filterposId);
		
		q.setFilter(filter);
		
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSCategory> GetAllSubCategories(long hotelId,long categoryId)
	{
		Query q = new Query(EntityName.POSCategory);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterparentCategory = new FilterPredicate("parentCategory",FilterOperator.EQUAL,categoryId);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterparentCategory);
		q.setFilter(filter);
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSCategory> GetAllCategories(long hotelId,long posId)
	{
		Query q = new Query(EntityName.POSCategory);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filter = null;
		if(posId==0){
			filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		}else{
			Filter filterparentCategory = new FilterPredicate("posId",FilterOperator.EQUAL,posId);
			filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterparentCategory);
		}
		q.setFilter(filter);
		
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSCategory> GetAllSubPOS(long hotelId,long posId)
	{
		Query q = new Query(EntityName.POSCategory);
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterposId = new FilterPredicate("posId",FilterOperator.EQUAL,posId);
		Filter filterparentCategory = new FilterPredicate("parentCategory",FilterOperator.EQUAL,0);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable, filterposId, filterparentCategory);
		
		q.setFilter(filter);
		
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<POSCategory> GetPOSCategoryList(List<Entity> entities)
	{
		List<POSCategory> list = new ArrayList<POSCategory>();
		for(Entity e : entities)
		{
			POSCategory a = new POSCategory();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public POSCategory AddCategories(long id,String title,String pospoint,String taxuse,String des) throws EntityNotFoundException{
		POSCategory a = new POSCategory();
		a.setHotelId(id);
		a.setTitle(title);
		a.setPOSId(Long.parseLong(pospoint));
		a.setDescription(des);
		a.setAvaliable(true);
		a.setStatus(true);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public POSCategory AddSubCategories(long id,String title,String pospoint,String taxuse,String des,String idcategory) throws EntityNotFoundException{
		POSCategory a = new POSCategory();
		a.setHotelId(id);
		a.setTitle(title);
		a.setPOSId(Long.parseLong(pospoint));
		a.setParentCategory(Long.parseLong(idcategory));
		a.setDescription(des);
		a.setAvaliable(true);
		a.setStatus(true);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public POSCategory EditCategories(long categoryId,String title,String taxuse,String des) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoryId);
		a.setTitle(title);
		a.setDescription(des);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public POSCategory EditSubCategories(long categoryId,String newSubCategory) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoryId);
		a.setSubCategoryString(newSubCategory);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public POSCategory EditParentCategories(long categoryId) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoryId);
		a.setParentCategory(0);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	public POSCategory EditCategoriesforSub(long categoryId,String title,String des,String idsub) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoryId);
		String idsubSet = "";
		if(a.getSubCategoryString()!=null&&!a.getSubCategoryString().equals("")){
			idsubSet = a.getSubCategoryString()+","+idsub;
		}else{
			idsubSet = idsub;
		}
		a.setSubCategoryString(idsubSet);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public POSCategory ChangeStatus(long categoriesId) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoriesId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSCategory DeleteCategories(long categoriesId) throws EntityNotFoundException{
		POSCategory a = GetPOSCategory(categoriesId);
		a.setAvaliable(false);
		a.setStatus(false);
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
	
	public POSCategory UpdatePOSCategory(POSCategory cat)
	{
		Key key = datastore.put(cat.getEntity());
		cat.setId(key.getId());
		cat.setKey(key);
		return cat;
	}
	
	public List<POSCategory> GetAllCategoryInPOS(long posId){
		Query q = new Query(EntityName.POSCategory);
		Filter filter = new FilterPredicate("posId",FilterOperator.EQUAL,posId);
		q.setFilter(filter);
		
		return GetPOSCategoryList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
}
